home *** CD-ROM | disk | FTP | other *** search
- /* AIFF/AIFC */
- /* IMPORT/EXPORT */
- /* v 1.0 */
- /* 1996 by ANR */
-
- #include "PPPlug.h"
- #include "AIFF.h"
- #include "sound.h"
- #include "soundinput.h"
-
- #if defined(powerc) || defined(__powerc)
- enum {
- PlayerPROPlug = kCStackBased
- | RESULT_SIZE(SIZE_CODE( sizeof(OSErr)))
- | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
- | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( InstrData*)))
- | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( sData**)))
- | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( short*)))
- | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( FSSpec*)))
- | STACK_ROUTINE_PARAMETER(6, SIZE_CODE(sizeof( PPInfoPlug*)))
- };
-
- ProcInfoType __procinfo = PlayerPROPlug;
- #else
- #include <A4Stuff.h>
- #endif
-
- Ptr AIFFtoSnd( Ptr sound,
- long *loopStart,
- long *loopEnd,
- short *sampleSize,
- unsigned long *rate)
- {
- long i, SizeH, numSampleFrames, StartId, numChannels;
- ContainerChunk *CC;
- ChunkHeader *CH;
- ExtCommonChunk *CommC;
- SoundDataChunk *SData;
- MarkerChunk *marker;
- Marker *mm;
- unsigned long theFixed;
- Boolean Compression = false;
- ID compType;
-
- CC = (ContainerChunk*) sound;
-
- *loopStart = *loopEnd = 0;
- *sampleSize = 8;
- *rate = rate22khz;
- numChannels = 1;
-
- if( CC->formType == AIFCID) Compression = true;
- else if( CC->formType == AIFFID) Compression = false;
-
- for( i = sizeof( ContainerChunk); i < CC->ckSize;)
- {
- CH = (ChunkHeader*) (sound + i);
- if( CH->ckID == CommonID)
- {
- CommC = (ExtCommonChunk*) CH;
- numChannels = CommC->numChannels;
- numSampleFrames = CommC->numSampleFrames;
- compType = CommC->compressionType;
-
- {
- struct _extended80 *too;
-
- too = (struct _extended80* ) &CommC->sampleRate;
-
- *rate = *((unsigned long*) too->man);
- *rate >>= (15L - (too->exp - 0x3FFFL));
- }
-
- *sampleSize = CommC->sampleSize;
- if( *sampleSize == 16) numSampleFrames *= 2;
-
- }
- else if( CH->ckID == SoundDataID)
- {
- SData = (SoundDataChunk*) CH;
- StartId = i;
- }
- else if( CH->ckID == MarkerID)
- {
- marker = (MarkerChunk*) CH;
- if( marker->numMarkers == 2)
- {
- *loopStart = marker->Markers[ 0].position;
- mm = (Marker*) marker->Markers;
- mm = (Marker*) ((Ptr) mm + 8L);
- mm = (Marker*) ((Ptr) mm + marker->Markers[ 0].markerName[0]);
- *loopEnd = mm->position;
-
- if( *sampleSize == 16)
- {
- *loopStart *= 2;
- *loopEnd *= 2;
- }
- }
- }
-
- i += CH->ckSize;
- i += 8;
- }
-
- StartId += 16; /* Size of the SoundDataChunk beginning */
-
- SizeH = GetPtrSize( sound);
- if( numChannels == 1)
- {
- BlockMove( sound + StartId, sound, SizeH - StartId);
- }
- else
- {
- if( *sampleSize == 8)
- {
- for( i = 0; i < SizeH - StartId; i++)
- {
- (sound)[ i] = (sound + StartId)[ i * numChannels];
- }
- }
- else
- {
- for( i = 0; i < (SizeH - StartId)/2; i++)
- {
- ((short*) sound)[ i] = ((short*) (sound + StartId))[ i * numChannels];
- }
- }
- }
-
- if( Compression)
- {
- switch( compType)
- {
- case MACE3Type:
- sound = MyExp1to3( sound, numSampleFrames);
- numSampleFrames *= 3;
-
- ConvertInstrumentIn( (Byte*) sound, numSampleFrames);
- break;
-
- case MACE6Type:
- sound = MyExp1to6( sound, numSampleFrames);
- numSampleFrames *= 6;
-
- ConvertInstrumentIn( (Byte*) sound, numSampleFrames);
- break;
-
- default:
- return 0L;
- break;
- }
- }
-
- SetPtrSize( sound, numSampleFrames);
-
- return sound;
- }
-
- OSErr TestAIFF( ContainerChunk* CC)
- {
- if( CC->formType == AIFCID) return noErr;
- else if( CC->formType == AIFFID) return noErr;
- else return MADFileNotSupportedByThisPlug;
- }
-
- OSErr main( OSType order, // Order to execute
- InstrData *InsHeader, // Ptr on instrument header
- sData **sample, // Ptr on samples data
- short *sampleID, // If you need to replace/add only a sample, not replace the entire instrument (by example for 'AIFF' sound)
- // If sampleID == -1 : add sample else replace selected sample.
- FSSpec *AlienFileFSSpec, // IN/OUT file
- PPInfoPlug *thePPInfoPlug)
- {
- OSErr myErr = noErr;
- Ptr AlienFile;
- short iFileRefI;
- long inOutBytes;
-
- #ifndef powerc
- long oldA4 = SetCurrentA4(); //this call is necessary for strings in 68k code resources
- #endif
-
- switch( order)
- {
- case 'PLAY':
- {
- Ptr theSound;
- long lS, lE;
- short sS;
- unsigned long rate;
-
- myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
- if( myErr == noErr)
- {
- GetEOF( iFileRefI, &inOutBytes);
-
- theSound = NewPtr( inOutBytes);
- if( theSound == 0L) myErr = MADNeedMemory;
- else
- {
- FSRead( iFileRefI, &inOutBytes, theSound);
-
- theSound = AIFFtoSnd( theSound, &lS, &lE, &sS, &rate);
-
- if( theSound != 0L)
- {
- myErr = CallRPlaySoundUPP( theSound, GetPtrSize( theSound), 0, 0xFF, sS, lS, lE, rate);
-
- DisposePtr( theSound);
- theSound = 0L;
- }
- }
-
- FSClose( iFileRefI);
- }
- }
- break;
-
- case 'IMPL':
- {
- Ptr theSound;
- long lS, lE;
- short sS;
- unsigned long rate;
-
- myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
- if( myErr == noErr)
- {
- GetEOF( iFileRefI, &inOutBytes);
-
- theSound = NewPtr( inOutBytes);
- if( theSound == 0L) myErr = MADNeedMemory;
- else
- {
- FSRead( iFileRefI, &inOutBytes, theSound);
-
- theSound = AIFFtoSnd( theSound, &lS, &lE, &sS, &rate);
-
- AddSoundToMAD( theSound, lS, lE, sS, 60, rate, AlienFileFSSpec->name, InsHeader, sample, sampleID);
- }
-
- FSClose( iFileRefI);
- }
- }
- break;
-
- case 'TEST':
- {
- Ptr theSound;
-
- myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
- if( myErr == noErr)
- {
- inOutBytes = 50L;
- theSound = NewPtr( inOutBytes);
- if( theSound == 0L) myErr = MADNeedMemory;
- else
- {
- FSRead( iFileRefI, &inOutBytes, theSound);
-
- myErr = TestAIFF( (ContainerChunk*) theSound);
- }
-
- DisposPtr( theSound);
-
- FSClose( iFileRefI);
- }
- }
- break;
-
- case 'EXPL':
- if( *sampleID >= 0)
- {
- OSType compType = 'NONE';
- unsigned long rate;
- sData *curData = sample[ *sampleID];
-
- myErr = FSpCreate( AlienFileFSSpec, 'TVOD', 'AIFF', smCurrentScript);
- myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
-
- if( myErr == noErr)
- {
- inOutBytes = curData->size;
- rate = curData->c2spd;
-
- myErr = SetupAIFFHeader( iFileRefI,
- 1,
- rate << 16L,
- curData->amp,
- compType,
- inOutBytes,
- 0);
-
- myErr = FSWrite( iFileRefI, &inOutBytes, curData->data);
- FSClose( iFileRefI);
- }
- }
- break;
-
- default:
- myErr = MADOrderNotImplemented;
- break;
- }
-
- #ifndef powerc
- SetA4( oldA4);
- #endif
-
- return myErr;
- }